package pro.shushi.pamirs.demo.core.action;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pro.shushi.pamirs.demo.api.enumeration.ItemStatusEnum;
import pro.shushi.pamirs.demo.api.model.DemoItem;
import pro.shushi.pamirs.demo.api.model.DemoItemCategory;
import pro.shushi.pamirs.demo.api.model.DemoItemStatistics;
import pro.shushi.pamirs.demo.api.pmodel.DemoItemProxy;
import pro.shushi.pamirs.demo.api.service.DemoItemService;
import pro.shushi.pamirs.demo.core.manager.CustomSqlManager;
import pro.shushi.pamirs.framework.connectors.data.sql.Pops;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.annotation.Model;
import pro.shushi.pamirs.meta.api.dto.condition.Pagination;
import pro.shushi.pamirs.meta.api.dto.wrapper.IWrapper;
import pro.shushi.pamirs.meta.constant.FunctionConstants;
import pro.shushi.pamirs.meta.enmu.FunctionCategoryEnum;
import pro.shushi.pamirs.meta.enmu.FunctionOpenEnum;
import pro.shushi.pamirs.meta.enmu.FunctionTypeEnum;
import pro.shushi.pamirs.meta.util.TypeUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
@Model.model(DemoItemProxy.MODEL_MODEL)
public class DemoItemProxyAction {

    @Autowired
    private DemoItemService demoItemService;
    @Autowired
    private CustomSqlManager customSqlManager;

    @Function.Advanced(type = FunctionTypeEnum.QUERY, displayName = "商品统计列表",category = FunctionCategoryEnum.QUERY_PAGE)
    @Function.fun(FunctionConstants.queryPage)
    @Function(openLevel = {FunctionOpenEnum.LOCAL, FunctionOpenEnum.REMOTE, FunctionOpenEnum.API})
    public Pagination<DemoItemProxy> queryPage(Pagination<DemoItemProxy> page, IWrapper<DemoItemProxy> queryWrapper) {
        List<DemoItemProxy> list = new ArrayList<>();

            List<DemoItem> items = demoItemService.queryListByWrapper(Pops.<DemoItem>lambdaQuery()
                    .from(DemoItem.MODEL_MODEL)
                    .eq(DemoItem::getStatus, ItemStatusEnum.ACTIVE));
            if(CollectionUtils.isEmpty(items)) return page;
            Map<Long, List<DemoItem>> itemMap = items.stream().collect(Collectors.groupingBy(DemoItem::getCategoryId));

            for (Map.Entry<Long, List<DemoItem>> longListEntry : itemMap.entrySet()) {
                long categoryId = longListEntry.getKey();
                List<DemoItem> demoItems = longListEntry.getValue();

                DemoItemCategory itemCategory = new DemoItemCategory().queryById(categoryId);
                DemoItemProxy itemStatistics = new DemoItemProxy();
                itemStatistics.setItemPrice(BigDecimal.valueOf(demoItems.stream().mapToDouble(t->t.getItemPrice().floatValue()).average().getAsDouble()));
                itemStatistics.setInventoryQuantity(demoItems.stream().mapToLong(t->t.getInventoryQuantity()).max().getAsLong());
                list.add(itemStatistics);
            }
            page.setContent(list);
            Integer count = CollectionUtils.isNotEmpty(list)?list.size():0;
            page.setSize(count.longValue());
        return page;
    }

}
